home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / SoundSprocketTest / TS3Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  7.7 KB  |  326 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Window.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Copyright © 1996 Apple Computer, Inc.
  6.  */
  7.  
  8. #include <assert.h>
  9.  
  10. #include "TS3Window.h"
  11.  
  12. enum {
  13.     kWindowDataThumbprint = 'wdat'
  14. };
  15.  
  16.  
  17. typedef struct WindowData {
  18.     unsigned long        thumbprint;
  19.     void*                method[kWindowMethod_COUNT];
  20. } WindowData;
  21.  
  22.  
  23. static WindowData** Window_GetData(
  24.     WindowPtr            inWindow);
  25.  
  26. static WindowMethodPtr Window_GetMethod(
  27.     WindowPtr            inWindow,
  28.     WindowMethod        inMethod);
  29.  
  30.  
  31. /* =============================================================================
  32.  *        Window_Init (external)
  33.  *
  34.  *    Initializes our window stuff.
  35.  * ========================================================================== */
  36. void Window_Init(
  37.     void)
  38. {
  39. }
  40.  
  41.  
  42. /* =============================================================================
  43.  *        Window_Exit (external)
  44.  *
  45.  *    Cleans up.
  46.  * ========================================================================== */
  47. void Window_Exit(
  48.     void)
  49. {
  50. }
  51.  
  52.  
  53. /* =============================================================================
  54.  *        Window_GetData (internal)
  55.  *
  56.  *    Returns the WindowData record associated with this window.  Returns NULL
  57.  *    if the window doesn't have a WindowData record associated with it.
  58.  * ========================================================================== */
  59. WindowData** Window_GetData(
  60.     WindowPtr            inWindow)
  61. {
  62.     WindowData**        windowData;
  63.     
  64.     assert(inWindow != NULL);
  65.     
  66.     windowData = (WindowData**) GetWRefCon(inWindow);
  67.     
  68.     if (windowData != NULL && (*windowData)->thumbprint != kWindowDataThumbprint)
  69.     {
  70.         windowData = NULL;
  71.     }
  72.     
  73.     return windowData;
  74. }
  75.  
  76.  
  77. /* =============================================================================
  78.  *        Window_GetMethod (internal)
  79.  *
  80.  *    Returns the function pointer for the given method ID.  The result may be
  81.  *    NULL.
  82.  * ========================================================================== */
  83. WindowMethodPtr Window_GetMethod(
  84.     WindowPtr            inWindow,
  85.     WindowMethod        inMethod)
  86. {
  87.     WindowMethodPtr        result;
  88.     WindowData**        windowData;
  89.     
  90.     assert(inMethod >= kWindowMethod_FIRST);
  91.     assert(inMethod <  kWindowMethod_COUNT);
  92.     
  93.     result = NULL;
  94.     
  95.     windowData = Window_GetData(inWindow);
  96.     if (windowData != NULL)
  97.     {
  98.         result = (*windowData)->method[inMethod];
  99.     }
  100.     
  101.     return result;
  102. }
  103.  
  104.  
  105. /* =============================================================================
  106.  *        Window_New (external)
  107.  *
  108.  *    Makes a new WindowData structure for the window, points the window's refCon
  109.  *    at it, and fills it in using the metahandler.  The metahandler should return
  110.  *    a window method pointer that corresponds to the given method ID, or NULL
  111.  *    if none.
  112.  * ========================================================================== */
  113. void Window_New(
  114.     WindowPtr            inWindow,
  115.     WindowMethodPtr        (*inMetaHandler)(WindowMethod inMethod))
  116. {
  117.     WindowData**        windowData;
  118.     WindowMethod        method;
  119.     
  120.     assert(inWindow != NULL);
  121.     assert(inMetaHandler != NULL);
  122.     
  123.     // Allocate the window data
  124.     windowData = (WindowData**) NewHandle(sizeof(WindowData));
  125.     assert(windowData != NULL);
  126.     
  127.     // Fill it in
  128.     (*windowData)->thumbprint = kWindowDataThumbprint;
  129.     
  130.     for (method = kWindowMethod_FIRST; method < kWindowMethod_COUNT; method++)
  131.     {
  132.         (*windowData)->method[method] = (*inMetaHandler)(method);
  133.     }
  134.     
  135.     // Point the window refcon at it
  136.     SetWRefCon(inWindow, (long) windowData);
  137. }
  138.  
  139.  
  140. /* =============================================================================
  141.  *        Window_Dispose (external)
  142.  *
  143.  *    Disposes of the window's WindowData structure.
  144.  * ========================================================================== */
  145. void Window_Dispose(
  146.     WindowPtr            inWindow)
  147. {
  148.     WindowData**        windowData;
  149.     
  150.     windowData = Window_GetData(inWindow);
  151.     if (windowData != NULL)
  152.     {
  153.         DisposeHandle((Handle) windowData);
  154.         SetWRefCon(inWindow, 0);
  155.     }
  156. }
  157.  
  158.  
  159. /* =============================================================================
  160.  *        Window_GetSleep (external)
  161.  *
  162.  *    Sets *outSleep to the value that should be passed to WaitNextEvent when this
  163.  *    is the front window.
  164.  * ========================================================================== */
  165. void Window_GetSleep(
  166.     WindowPtr            inWindow,
  167.     UInt32*                outSleep)
  168. {
  169.     WindowMethodPtr        getSleepMethod;
  170.     
  171.     assert(outSleep != NULL);
  172.     
  173.     *outSleep = 0xFFFFFFFF;
  174.     
  175.     if (inWindow != NULL)
  176.     {
  177.         getSleepMethod = Window_GetMethod(inWindow, kWindowMethod_GetSleep);
  178.         if (getSleepMethod != NULL)
  179.         {
  180.             (*getSleepMethod)(inWindow, outSleep);
  181.         }
  182.     }
  183. }
  184.  
  185.  
  186. /* =============================================================================
  187.  *        Window_ConsumeEvent (external)
  188.  *
  189.  *    Sets *outConsumed to true if the window consumed the event, or false if not.
  190.  *    Overriding this method allows the window to do idle time things at null-
  191.  *    event times, or dialog things.
  192.  * ========================================================================== */
  193. void Window_ConsumeEvent(
  194.     WindowPtr            inWindow,
  195.     const EventRecord*    inEvent,
  196.     Boolean*            outConsumed)
  197. {
  198.     WindowMethodPtr        consumeEventMethod;
  199.     
  200.     assert(inEvent != NULL);
  201.     assert(outConsumed != NULL);
  202.     
  203.     *outConsumed = false;
  204.     
  205.     if (inWindow != NULL)
  206.     {
  207.         consumeEventMethod = Window_GetMethod(inWindow, kWindowMethod_ConsumeEvent);
  208.         if (consumeEventMethod != NULL)
  209.         {
  210.             (*consumeEventMethod)(inWindow, inEvent, outConsumed);
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. /* =============================================================================
  217.  *        Window_MouseDown (external)
  218.  *
  219.  *    Processes a mouse-down in the window content area.
  220.  * ========================================================================== */
  221. void Window_MouseDown(
  222.     WindowPtr            inWindow,
  223.     Point                inWhere)
  224. {
  225.     WindowMethodPtr        mouseDownMethod;
  226.     
  227.     if (inWindow != NULL)
  228.     {
  229.         mouseDownMethod = Window_GetMethod(inWindow, kWindowMethod_MouseDown);
  230.         if (mouseDownMethod != NULL)
  231.         {
  232.             (*mouseDownMethod)(inWindow, inWhere);
  233.         }
  234.     }
  235. }
  236.  
  237.  
  238. /* =============================================================================
  239.  *        Window_KeyDown (external)
  240.  *
  241.  *    Processes a key-down or auto-key event.
  242.  * ========================================================================== */
  243. void Window_KeyDown(
  244.     WindowPtr            inWindow,
  245.     char                inChar,
  246.     char                inKeyCap,
  247.     short                inModifiers,
  248.     Boolean                inAutoKey)
  249. {
  250.     WindowMethodPtr        keyDownMethod;
  251.     
  252.     if (inWindow != NULL)
  253.     {
  254.         keyDownMethod = Window_GetMethod(inWindow, kWindowMethod_KeyDown);
  255.         if (keyDownMethod != NULL)
  256.         {
  257.             (*keyDownMethod)(inWindow, inChar, inKeyCap, inModifiers, inAutoKey);
  258.         }
  259.     }
  260. }
  261.  
  262.  
  263. /* =============================================================================
  264.  *        Window_Update (external)
  265.  *
  266.  *    Redraws the contents of the window in response to an update event.
  267.  * ========================================================================== */
  268. void Window_Update(
  269.     WindowPtr            inWindow)
  270. {
  271.     WindowMethodPtr        updateMethod;
  272.     
  273.     if (inWindow != NULL)
  274.     {
  275.         updateMethod = Window_GetMethod(inWindow, kWindowMethod_Update);
  276.         if (updateMethod != NULL)
  277.         {
  278.             (*updateMethod)(inWindow);
  279.         }
  280.     }
  281. }
  282.  
  283.  
  284. /* =============================================================================
  285.  *        Window_Activate (external)
  286.  *
  287.  *    Handles window activation.
  288.  * ========================================================================== */
  289. void Window_Activate(
  290.     WindowPtr            inWindow)
  291. {
  292.     WindowMethodPtr        activateMethod;
  293.     
  294.     if (inWindow != NULL)
  295.     {
  296.         activateMethod = Window_GetMethod(inWindow, kWindowMethod_Activate);
  297.         if (activateMethod != NULL)
  298.         {
  299.             (*activateMethod)(inWindow);
  300.         }
  301.     }
  302. }
  303.  
  304.  
  305. /* =============================================================================
  306.  *        Window_Deactivate (external)
  307.  *
  308.  *    Handles window deactivation.
  309.  * ========================================================================== */
  310. void Window_Deactivate(
  311.     WindowPtr            inWindow)
  312. {
  313.     WindowMethodPtr        deactivateMethod;
  314.     
  315.     if (inWindow != NULL)
  316.     {
  317.         deactivateMethod = Window_GetMethod(inWindow, kWindowMethod_Deactivate);
  318.         if (deactivateMethod != NULL)
  319.         {
  320.             (*deactivateMethod)(inWindow);
  321.         }
  322.     }
  323. }
  324.  
  325.  
  326.